package com.api.gbhg.zengping.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.zengping.dao.BudgetAdjustmentDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.entity.Info;
import com.api.gbhg.zengping.page.PageInfo;
import com.api.gbhg.zengping.utils.CodeCreateWorkFlow;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @program: gbhg-oa
 * @description: 预算调整相关API
 * @author: Mr.zeng
 * @create: 2021-02-02 11:08
 **/
public class BudgetAdjustmentAction {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    BudgetAdjustmentDao adjustmentDao=new BudgetAdjustmentDao();
    private final PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    /**
     * 预算调整列表 (所有已归档的流程)
     * @param param
     * @return
     */
    @POST
    @Path("/getBudgetAdjustmentList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getBudgetHpList(Map<String,Object> param) {
        String xm = BudgetingListAction.getProjectNameReturnType((String) param.get("xm"));
        if(!Strings.isNullOrEmpty(xm)){
            param.put("xm",xm);
        }
        RecordSet rs = adjustmentDao.selectBylcztzList(param);
        HashMap<String,Object>[] hashMaps=new HashMap[rs.getArray().size()];
        int i=0;
        // 固定申报项目列表
        Set<String> set = BudgetingListAction.getIsGDSBProject();
        while (rs.next()){
            hashMaps[i]=new HashMap<>();
            hashMaps[i].put("requestId",rs.getString("lcjlsqjlid"));
            hashMaps[i].put("lcbj",rs.getString("lcbj"));
            boolean flag = set.contains(rs.getString("lcbj"));
            // 是否固定申报项目
            hashMaps[i].put("isGDSBXM",flag==true?"是":"否");
            hashMaps[i].put("xmmc",rs.getString("xmmc"));
            //调整原因
            hashMaps[i].put("tzyy",rs.getString(""));
            //编报人
            RecordSet jbr = rmcd.getUserNameById(rs.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }
            hashMaps[i].put("bbr",loginid);
            // 申报单位
            RecordSet sbdw = rmcd.getSBDWById(rs.getInt("sbdw"));
            sbdw.next();
            hashMaps[i].put("dw",sbdw.getString("SUBCOMPANYNAME"));
            // 申报科室
            RecordSet sbks = rmcd.getSBKSById(rs.getInt("sbks"));
            sbks.next();
            hashMaps[i].put("bm",sbks.getString("departmentmark"));
            // 项目级别名称
            hashMaps[i].put("xmjbmc",rs.getString(""));
            hashMaps[i].put("yszje",rs.getString("yszje"));
            hashMaps[i].put("isSP",rs.getString(""));
            hashMaps[i].put("SPZt",rs.getString(""));
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo();
        RecordSet recordSet = adjustmentDao.selectBylcztzCount(param);
        recordSet.next();
        pageInfo.setTotal(recordSet.getInt("num"));
        pageInfo.setPageNum((Integer) param.getOrDefault("pageNum",0));
        pageInfo.setPageSize((Integer) param.getOrDefault("pageSize",0));
        pageInfo.setList(hashMaps);
        return JSON.toJSONString(pageInfo);
    }

    /**
     * 预算调整流程
     * @param
     * @return
     */
    @POST
    @Path("/getBudgetAdjustmentWorkFlow")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getBudgetAdjustmentWorkFlow(Map<String,Object> param) {
        Info[] infos = new TestGetInfo().DetailsData(param);
        Info info=new Info();
        for (int i = 0; i < infos.length; i++) {
            switch (infos[i].getTitle()){
                case "tab":
                    info.setTable(infos[i].getTable());
                    break;
                case "dt1":
                    info.setData(infos[i].getData());
                    break;
                case "dt2":
                    info.setData1(infos[i].getData1());
                    break;
                case "dt3":
                    info.setData2(infos[i].getData2());
                    break;
                case "dt4":
                    info.setData3(infos[i].getData3());
                    break;
                default:
                    break;
            }
        }
        // 將原來的流程置為不可用
        Map<String, Object> table = info.getTable();
        String requestId = (String) table.getOrDefault("requestId",null);
        Map<String,Object> map=new HashMap<>();
        if(requestId!=null){
            boolean b = adjustmentDao.updateUfJFVersion(requestId);
            map.put("flag",b);
        }
        String newRequestId = new CodeCreateWorkFlow().createWorkFlow(info);
        map.put("requestId",newRequestId);
//        String s = new Test2().webService(1, getMap(info.getTable()), "1");
//        String s= Test2.runerrorlogwfid(getMap(info.getTable()),info.getData());
        return JSON.toJSONString(map);

    }





    public Map<String,String> getMap(Map<String,Object> map){
        Map<String,String> map1=new HashMap<>();
        for (String o:map.keySet()
             ) {

            map1.put(o,map.get(o).toString());
        }
        return map1;
    }

}
